package com.springbootblog.service.serviceImpl.fontend;

import com.springbootblog.service.fontend.ImUploadService;
import com.springbootblog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class ImUploadServiceImpl implements ImUploadService
{
    /**
     * 上传文件存放目录
     */
    @Value("${spring.upload-path}")
    private String basePath;

    /**
     * 图片url
     */
    @Value("${spring.upload-url}")
    private String uploadUrl;

    @Override
    public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request)
    {
        if (file != null && !file.isEmpty())
        {
            /*HttpSession session = request.getSession();
            System.out.println(session);
            Integer userid = (Integer)session.getAttribute("userid");//*/
            // 获取token
            String token = request.getHeader(JwtUtils.token_name);
            // 解析token
            Claims paramList = JwtUtils.parseJWT(token);
            Integer userid;
            if(paramList == null || paramList.equals(null))
            {
                userid = null;
            }
            else
            {
                userid = (Integer) paramList.get("userid");
            }
            //Integer userid = (Integer) paramList.get("userid");
            System.out.println(userid);
            if(userid == null)
            {
                userid = 0;
            }
            System.out.println(userid);
            // =====================================================================
            // 文件上传逻辑
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件内容类型
            String contentType = file.getContentType();
            // 获取文件大小
            long fileSize = file.getSize();

            //声明一个指定目录,用来存放上传的文件
            String realPath = basePath + userid + "/";
            // 创建目录
            File dir = new File(realPath);
            if(!dir.exists())
            {
                dir.mkdirs();
                System.out.println("目录创建中！");
            }
            System.out.println("目录创建之后");
            // 重命名文件 每个上传的文件名不重复UUID, 当前时间的毫秒数
            String suffix = fileName.substring(fileName.lastIndexOf(".")); //1.jpg  .jpg
            // 新的文件名
            fileName = UUID.randomUUID()+suffix;
            System.out.println(fileName);
            try
            {
                System.out.println("上传文件");
                file.transferTo(new File(dir,fileName));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            Map<String,Object> array = new HashMap<>(3);
            array.put("url", uploadUrl + userid + "/" +fileName);
            array.put("alt", "");
            array.put("href", "");

            Map<String,Object> result = new HashMap<>(2);
            result.put("errno",0);
            result.put("data",array);
            return result;
        }
        else
        {
            // 文件为空处理逻辑
            Map<String,Object> result = new HashMap<>(2);
            result.put("code",-1);
            result.put("msg","文件流为空！");
            return result;
        }
    }

    @Override
    public Map<String, Object> uploadVideo(MultipartFile file, HttpServletRequest request)
    {
        return null;
    }
}
